package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.ConfigConstant;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.enums.CustomCardEnum;
import com.xbongbong.paas.enums.clazz.CustomCardClass;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.pojo.CustomCardPoJo;
import com.xbongbong.paas.pojo.dto.CardListDTO;
import com.xbongbong.paas.pojo.dto.CardSaveDTO;
import com.xbongbong.paas.pojo.vo.CardListVO;
import com.xbongbong.paas.pojo.vo.CardSaveVO;
import com.xbongbong.paas.service.CustomCardService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.sys.domain.entity.UserConfigEntity;
import com.xbongbong.sys.model.UserConfigModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.xbongbong.paas.enums.CustomCardEnum.REPORT_FORM;

/**
 * @author: wufeng
 * @date: 2018/8/30 15:49
 * @desrcption: 自定义卡片实现类
 */
@Service("customCardService")
public class CustomCardServiceImpl implements CustomCardService {

    private static final Logger LOG = LoggerFactory.getLogger(CustomCardServiceImpl.class);



    @Resource
    private UserConfigModel userConfigModel;
    @Resource
    private PaasMenuModel paasMenuModel;

    @Override
    public CardSaveVO save(CardSaveDTO cardSaveDTO) throws XbbException {
        CardSaveVO cardSaveVO = new CardSaveVO();
        try {
            if (cardSaveDTO.getUserConfigId() == null) {
                UserConfigEntity userConfigEntity = new UserConfigEntity();
                BeanUtils.copyProperties(cardSaveDTO, userConfigEntity);
                userConfigEntity.setConfigAlias(JSONObject.toJSONString(cardSaveDTO.getCardList()));
                String configName = ConfigConstant.CUSTOM_CARD + "_" + cardSaveDTO.getAppId().toString();
                userConfigEntity.setConfigName(configName);
                userConfigModel.insert(userConfigEntity);
                cardSaveDTO.setUserConfigId(userConfigEntity.getId());
            } else {
                String corpid = cardSaveDTO.getCorpid();
                UserConfigEntity userConfigEntity = userConfigModel.getByKey(cardSaveDTO.getUserConfigId(), corpid);
                if (userConfigEntity == null) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002, MessageConstant.CONFIG_NOT_EXIST);
                }
                UserConfigEntity entity = new UserConfigEntity();
                BeanUtils.copyProperties(cardSaveDTO, entity);
                entity.setConfigAlias(JSONObject.toJSONString(cardSaveDTO.getCardList()));
                entity.setId(cardSaveDTO.getUserConfigId());
                userConfigModel.update(entity);
                cardSaveDTO.setUserConfigId(cardSaveDTO.getUserConfigId());
            }
        }  catch (XbbException e) {
            throw e;
        }  catch (Exception e) {
            LOG.error("commonForm数据库设置卡片出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }

        return cardSaveVO;
    }

    @Override
    public CardListVO list(CardListDTO cardListDTO) throws XbbException {
        CardListVO cardListVO = new CardListVO();
        try {
            String userId = cardListDTO.getUserId();
            String corpid = cardListDTO.getCorpid();
            String configName = ConfigConstant.CUSTOM_CARD + "_" + cardListDTO.getAppId().toString();
            UserConfigEntity userConfigEntity = userConfigModel.getByUserId(userId, corpid, configName);
            if (userConfigEntity == null) {
                userConfigEntity = new UserConfigEntity();
            }
            List<CustomCardPoJo> userConfigList = JSONObject.parseArray(userConfigEntity.getConfigAlias(), CustomCardPoJo.class);

            cardListVO.setUserConfigId(userConfigEntity.getId());
            // 全部卡片
            List<CustomCardPoJo> cardList = new ArrayList<>();
            List<CustomCardClass> customCardList = CustomCardEnum.getCardList();
            for (CustomCardClass card: customCardList) {
                if(!card.getType().equals(REPORT_FORM.getType())) {
                    CustomCardPoJo cardPoJo = new CustomCardPoJo();
                    cardPoJo.setType(card.getType());
                    cardPoJo.setName(card.getMemo());
                    cardList.add(cardPoJo);
                }
            }
            Map<String, Object> params = BeanUtil.convertBean2Map(cardListDTO);
            params.put("type", "4");
            params.put("userId", cardListDTO.getUserId());
            params.put("orderByStr", "sort");
            List<PaasMenuEntity> list = paasMenuModel.list(params);
            if (list == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.MENU_NOT_EXIST);
            }
            for(PaasMenuEntity menu: list){
                CustomCardPoJo cardPoJo = new CustomCardPoJo();
                cardPoJo.setId(menu.getId());
                cardPoJo.setType(REPORT_FORM.getType());
                cardPoJo.setName(menu.getName());
                cardList.add(cardPoJo);
            }
            List<CustomCardPoJo> userCardList = new ArrayList<>();
            if (userConfigList != null) {
                for (CustomCardPoJo cardPoJo: cardList) {
                    Integer cardType = cardPoJo.getType();
                    for(CustomCardPoJo userCard : userConfigList){
                        Integer userCardType = userCard.getType();
                        if(cardType.equals(userCardType)){
                            if(!userCardType.equals(REPORT_FORM.getType())){
                                cardPoJo.setName(CustomCardEnum.getMemo(userCardType));
                            }
                            userCardList.add(cardPoJo);
                        }
                    }
                }
            }

            cardListVO.setUserCardList(userCardList);
            cardListVO.setCardList(cardList);
        }  catch (XbbException e) {
            throw e;
        }  catch (Exception e){
            LOG.error("commonForm数据库获取卡片列表出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return cardListVO;
    }
}
